//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.mercury.emqx.client.interceptor;

import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;

import java.io.IOException;

/**
 * retrofit 拦截器 拦截所有要发送的请求并且往请求头添加Authorization
 *
 * @author mercury
 */
@EqualsAndHashCode(callSuper = true)
@Component
@Data
@NoArgsConstructor
public class MqttAuthorizationInterceptor extends BasePathMatchInterceptor {

    private static final Logger log = LoggerFactory.getLogger(MqttAuthorizationInterceptor.class);

    private String appId;

    private String appSecret;

    /**
     * 拦截器 Basic 认证
     * 想要访问emqx的http接口必须要往header中加上Authorization
     */
    @Override
    public Response doIntercept(Chain chain) throws IOException {
        Request request = chain.request();
        String credentials = this.appId + ":" + this.appSecret;
        String authorization = "Basic " + Base64Utils.encodeToString(credentials.getBytes());

        request.newBuilder().

        Request newReq = request.newBuilder()
                .addHeader("Authorization", authorization)
                .addHeader("Accept", "application/json").build();
        return chain.proceed(newReq);
    }

}
